import { getReport } from '@/services/goods/report';
import { ProTable } from '@ant-design/pro-components';
import { useDispatch, useSelector } from '@umijs/max';
import { useRef } from 'react';
import EditForm from './EditForm';
import { columns } from './columns';
import './index.less';

const SearchTable = () => {
  const {
    qualityreport: { pageSize },
  } = useSelector((state) => state);
  const dispatch = useDispatch();
  const actionRef = useRef();
  const update = (data) => {
    dispatch({
      type: 'qualityreport/updateState',
      payload: data,
    });
  };

  const handleEdit = (data) => {
    dispatch({
      type: 'qualityreport/selectItem',
      payload: data,
    });
  };
  return (
    <>
      <ProTable
        columns={columns({ handleEdit })}
        actionRef={actionRef}
        cardBordered
        cardProps={{
          size: 'small',
          style: {
            padding: 0,
          },
        }}
        search={{
          labelWidth: 'auto',
          style: {
            padding: '24px 12px',
          },
        }}
        options={false}
        request={async (params = {}) => {
          const { current, pageSize } = params;
          const { code, data, total } = await getReport({
            page: current,
            pageSize,
            ...params,
            // categoryName: params?.categoryName,
            // level: params?.level,
          });
          let tableData = [];
          if (code === 200 && data) {
            //
            tableData =
              data.map((item) => {
                delete item?.children;
                return item;
              }) || [];
          }
          return { data: tableData, success: code === 200, total: total || 0 };
        }}
        rowKey="id"
        pagination={{
          pageSize: pageSize,
          onChange: (_, pageSize) => update({ pageSize }),
          showSizeChanger: true,
        }}
        headerTitle="质检报告"
        toolBarRender={null}
      />
      <EditForm actionRef={actionRef} />
    </>
  );
};

export default SearchTable;
